package com.winxuan.channel.service.commodity.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.winxuan.channel.model.commodity.CommodityExclude;
import com.winxuan.channel.service.commodity.ExcludeService;
import com.winxuan.support.dao.BaseDao;
import com.winxuan.support.dao.Finder;
import com.winxuan.support.dao.ParametersFinder;
import com.winxuan.util.pagination.Pagination;

@Service("excludeService")
public class ExcludeServiceImpl implements ExcludeService {

	private BaseDao baseDao;
	@Override
	public List<CommodityExclude> getExcludeCommodityByDistribute(Integer distribute,Integer commodity,Pagination pagination) {
		final Map<String,Integer> params = new HashMap<String,Integer>();
		params.put("distribute", distribute);
		params.put("commodity", commodity);
		Finder finder = new ParametersFinder(params) {
			@Override
			public String genQueryString() {
				StringBuffer sb=new StringBuffer("SELECT cl FROM CommodityExclude cl WHERE 1=1");
				if(params.get("distribute") != -1){
					sb.append(" AND cl.distribute=:distribute ");
				}
				if(params.get("commodity") != 0){
					sb.append(" AND cl.commodity=:commodity ");
				}
				return sb.toString();
			}
		};
		return baseDao.find(finder,pagination);
	}
	
	
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}


	@Override
	public boolean addExcludeCommodity(CommodityExclude commodityExclude) {
		Integer newId = (Integer) baseDao.save(commodityExclude);
		return newId != 0;
	}


	@Override
	public boolean updateExcludeCommodity(CommodityExclude commodityExclude) {
		baseDao.update(commodityExclude);
		return true;
	}


	@Override
	public boolean delelteExcludeCommodity(CommodityExclude commodityExclude) {
		baseDao.delete(commodityExclude);
		return true;
	}


	@Override
	public CommodityExclude getExcludeCommodity(Integer id) {
		CommodityExclude commodityExclude =  (CommodityExclude) baseDao.get(CommodityExclude.class, id);
		return commodityExclude;
	}
}
